package com.renwei.yunlong.pager;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.adapter.WorkAnnexAdapter;
import com.renwei.yunlong.base.BaseFragment;
import com.renwei.yunlong.bean.WorkAnnexBean;
import com.renwei.yunlong.http.HttpPresenter;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.ACache;
import com.zyf.fwms.commonlibrary.http.Api;

import java.util.List;

/**
 * 工单  附件  页面
 *
 * Created by Administrator on 2017/9/6.
 */

public class WorkAnnexPager extends BaseFragment implements View.OnClickListener,HttpTaskListener{

    private RecyclerView recyclerView;

    private WorkAnnexAdapter adapter;
    private ACache mCache;


    @Override
    public View initView() {
        View view = LayoutInflater.from(mActivity).inflate(R.layout.fragment_work_annex, null);

        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);

        mCache = ACache.get(getActivity());
        return view;
    }

    @Override
    public void initData() {


        //jsonBean={"correlationId":requestId工单ID,"type":类型}
        String requestId = mCache.getAsString("requestId");

        String companyType = mCache.getAsString("companyType");

        if ("1".equals(companyType)){//业主

//            String jsonBean = "{correlationId:\"" + requestId +"\",type:\"" +"1"+ "\"}";
//            ownerHttp(jsonBean);

        }else if ("2".equals(companyType)){//服务商

            String jsonBean = "{requestId:\"" + requestId +"\",type:\"" +"1"+ "\"}";
            serviceHttp(jsonBean);
            Log.e("服务商 工单附件jsonBean",jsonBean);
        }
    }

    private void setDataList(List<WorkAnnexBean.Rows> mList){
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new WorkAnnexAdapter(getActivity(),mList);
        recyclerView.setAdapter(adapter);
    }

    private void serviceHttp(String jsonBean) {
        HttpPresenter hp = new HttpPresenter(getActivity());
        hp.setUrl(Api.SERVICE_LIST_URL);
        hp.setshowDialogcontent("正在加载");
        hp.setRequsetId(2);
        hp.putParam("jsonBean", jsonBean);
        hp.setCallBack(this);
        hp.post();
    }

    private void ownerHttp(String jsonBean) {
        HttpPresenter hp = new HttpPresenter(getActivity());
        hp.setUrl(Api.OWNER_LIST_URL);
        hp.setshowDialogcontent("正在加载");
        hp.setRequsetId(1);
        hp.putParam("jsonBean", jsonBean);
        hp.setCallBack(this);
        hp.post();
    }


    @Override
    public void onClick(View v) {

    }

    @Override
    public void onSuccess(int requestId, String result) {
        switch (requestId){
            case 1:
                break;
            case 2:
                Log.e("服务商 工单附件数据",result);
                WorkAnnexBean serviceBean = new Gson().fromJson(result,WorkAnnexBean.class);
                if (serviceBean.getMessage().getCode() == 200){
                    setDataList(serviceBean.getRows());
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onException(int requestId, String code) {

    }
}
